const {ipcRenderer, ipcMain} = require('electron');

function define(values) {
    let obj = {};
    
    for(let i in values) {
        Object.defineProperty(obj, i,  {
            value:values[i],
            writeable:false
        });
    }
    
    return obj;
}

const consts = define({
    // Inter Process Communication
    ipc: define({
        toggle_window_visiable: 'toogle-window-visible'
    })
});


module.exports = class {
    static listenFromMain(key, callback) {
        ipcRenderer.on(key, callback);
        return this;
    }
    
    static listenFromRender(key, callback) {
        ipcMain.on(key, callback);
        return this;
    }
    
    static listen(key, callback) {
        ipcRenderer.on(key, callback);
        return this;
    }
    
    static send(key, args) {
        ipcRenderer.send(key, args);
        return this;
    }
    
    static sendToMain(key, args) {
        ipcRenderer.send(key, args);
        return this;
    }
    
    static sendToMainSync() {
        return ipcRenderer.sendSync(key, args);
    }
    
    static sendToRender(win, key, args) {
        win.webContents.send(key, args);
        return this;
    }
    
    //========
    static lfmToggleWindowVisiable(callback) {
        return this.listenFromMain(consts.ipc.toggle_window_visiable, callback);
    }
    
    static lfrToggleWindowVisiable(callback) {
        return this.listenFromRender(consts.ipc.toggle_window_visiable, callback);
    }
    
    static smToggleWindowVisiable(args) {
        return this.sendToMain(consts.ipc.toggle_window_visiable, args);
    }
    
    static smsyncToggleWindowVisiable(args) {
        return this.sendToMainSync(consts.ipc.toggle_window_visiable, args);
    }
    
    static srToggleWindowVisiable(win, args) {
        return this.sendToRender(win, consts.ipc.toggle_window_visiable, args);
    }
};
